package vn.allmobile.ambnews.activities.main;

import java.util.ArrayList;
import java.util.List;

import vn.allmobile.ambnews.R;
import vn.allmobile.ambnews.activities.list.onUserPaperClickListenner;
import vn.allmobile.ambnews.customview.FlipViewInterface;
import vn.allmobile.ambnews.data.Article;
import vn.allmobile.ambnews.data.Paper;
import vn.allmobile.ambnews.network.NetworkAPI;
import vn.allmobile.ambnews.network.NetworkChangeReceiver;
import vn.allmobile.ambnews.utils.Contants;
import vn.allmobile.ambnews.utils.ImageLoadOption;
import vn.allmobile.ambnews.utils.Log;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

public class HomeAdapter extends BaseAdapter implements OnClickListener {

	List<Paper> mListPapers;
	public Activity mAct;
	int _width, _height;
	private LayoutInflater inflater;
	//FlipViewController _flipview;
	//FlipView _flipview;
	FlipViewInterface _flipview;	
	@SuppressWarnings("deprecation")
	public HomeAdapter(Activity activity) {
		Display display = activity.getWindowManager().getDefaultDisplay();
		_width = display.getWidth();
		_height = display.getHeight();
		this.mAct = activity;		
		datachanged();
		inflater = LayoutInflater.from(activity);
	
	}

	public void datachanged() {
		if(NetworkAPI._userListPapers == null)
		{
			Log.v("userListPaper == null");
			this.mListPapers = new ArrayList<Paper>();
		}
		else
		{
			this.mListPapers = NetworkAPI._userListPapers;
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {		
		if (this.mListPapers.size() == 0) {
			return 1;
		} else {			
			return (int) Math.ceil((float) this.mListPapers.size()
					/ Contants.NUM_ITEM_GRIDVIEW) + 1;			
		}
	}

	@Override
	public int getItemViewType(int position)
	{
		return 0;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	
	
	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {		
		final Context context = parent.getContext();
		
		
		View localView = convertView;
		ViewHolder localViewHolder;
		
		
		if(localView == null)
		{
			localView = inflater.inflate(R.layout.home_page, parent,false);
			localViewHolder = new ViewHolder(); 
			localViewHolder.home_page1_hotnews_wrap = (View)localView.findViewById(R.id.home_page1_hotnews_wrap);
			localViewHolder.home_page1_hotnews_image = (ImageView)localView.findViewById(R.id.home_page1_hotnews_image);
			localViewHolder.home_page1_hotnews_title = (TextView)localView.findViewById(R.id.home_page1_hotnews_title);
			localViewHolder.loadingPane = (View)localView.findViewById(R.id.loadingPane);
			localViewHolder.home_page1_refreshButton = (ImageView)localView.findViewById(R.id.home_page1_refreshButton);
			
			localViewHolder.home_txt_square02 = (TextView)localView.findViewById(R.id.home_txt_square02);
			localViewHolder.home_img_square02 = (ImageView)localView.findViewById(R.id.home_img_square02);
			localViewHolder.home_shadow_square02 = (ImageView)localView.findViewById(R.id.home_shadow_square02);
			localViewHolder.home_loading_square02 = (View)localView.findViewById(R.id.home_loading_square02);
			
			localViewHolder.square_wrap  = (View)localView.findViewById(R.id.square_wrap);
			localViewHolder.square_txt_name = (TextView)localView.findViewById(R.id.square_txt_name);
						
			localViewHolder.txt_square[0] = (TextView)localView.findViewById(R.id.txt_square01);
			localViewHolder.img_square[0] = (ImageView)localView.findViewById(R.id.img_square01);
			localViewHolder.shadow_square[0] = (ImageView)localView.findViewById(R.id.shadow_square01);
			localViewHolder.loading[0] = (View)localView.findViewById(R.id.loading_01);
			localViewHolder.txt_square[1] = (TextView)localView.findViewById(R.id.txt_square02);
			localViewHolder.img_square[1] = (ImageView)localView.findViewById(R.id.img_square02);
			localViewHolder.shadow_square[1] = (ImageView)localView.findViewById(R.id.shadow_square02);
			localViewHolder.loading[1] = (View)localView.findViewById(R.id.loading_02);
			
			localViewHolder.txt_square[2] = (TextView)localView.findViewById(R.id.txt_square11);
			localViewHolder.img_square[2] = (ImageView)localView.findViewById(R.id.img_square11);
			localViewHolder.shadow_square[2] = (ImageView)localView.findViewById(R.id.shadow_square11);
			localViewHolder.loading[2] = (View)localView.findViewById(R.id.loading_11);
			localViewHolder.txt_square[3] = (TextView)localView.findViewById(R.id.txt_square12);
			localViewHolder.img_square[3] = (ImageView)localView.findViewById(R.id.img_square12);
			localViewHolder.shadow_square[3] = (ImageView)localView.findViewById(R.id.shadow_square12);
			localViewHolder.loading[3] = (View)localView.findViewById(R.id.loading_12);
			
			localViewHolder.txt_square[4] = (TextView)localView.findViewById(R.id.txt_square21);
			localViewHolder.img_square[4] = (ImageView)localView.findViewById(R.id.img_square21);
			localViewHolder.shadow_square[4] = (ImageView)localView.findViewById(R.id.shadow_square21);
			localViewHolder.loading[4] = (View)localView.findViewById(R.id.loading_21);
			localViewHolder.txt_square[5] = (TextView)localView.findViewById(R.id.txt_square22);
			localViewHolder.img_square[5] = (ImageView)localView.findViewById(R.id.img_square22);
			localViewHolder.shadow_square[5] = (ImageView)localView.findViewById(R.id.shadow_square22);
			localViewHolder.loading[5] = (View)localView.findViewById(R.id.loading_22);
			
			localViewHolder.grid_item_nointernet = (TextView)localView.findViewById(R.id.grid_item_nointernet);
			localViewHolder.grid_item_page = (TextView)localView.findViewById(R.id.grid_item_page);
			
			localViewHolder.settingButton = (ImageView)localView.findViewById(R.id.settingButton);
			localViewHolder.bookmarkButton  = (ImageView)localView.findViewById(R.id.bookmarkButton);
			localViewHolder.list_back_button  = (ImageView)localView.findViewById(R.id.list_back_button);
			localViewHolder.refreshButton  = (ImageView)localView.findViewById(R.id.refreshButton);
			
			setViewVisibility(localViewHolder, localView);
			localViewHolder.square_wrap.setVisibility(View.GONE);
			localViewHolder.home_page1_hotnews_wrap.setVisibility(View.GONE);			
			localView.setTag(localViewHolder);
		}
		else
		{
			localViewHolder = (ViewHolder) localView.getTag();
		}
		
		
		if(position == 0)
		{
			localViewHolder.home_page1_hotnews_wrap.setVisibility(View.VISIBLE);
			localViewHolder.square_wrap.setVisibility(View.GONE);
			if(NetworkChangeReceiver.isConnected(true) && NetworkAPI._userListPapers.size() > 0)
				localViewHolder.home_page1_refreshButton.setVisibility(View.VISIBLE);
			try{
				if(NetworkAPI._listHotArticles.size()>0)
				{
					
					Article hotnew = NetworkAPI._listHotArticles.get(0);
					String imageURL = "http://news.allmobile.vn/" + hotnew.getImage()
							+ "?mode=resize";
					imageURL += "&width=" + _width + "&height=" + _height;
					ImageLoader.getInstance().displayImage(imageURL, localViewHolder.home_page1_hotnews_image,
							ImageLoadOption.options, new ImageLoadingListener() {
							    @Override
							    public void onLoadingStarted(String imageUri, View view) {
							    	Log.v("loading...");				    
							    }
							    @Override
							    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
							    	((ImageView) view).setImageResource(R.drawable.no_image_220x220);
							    	_flipview.refreshPage(position);
							    }
							    @Override
							    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
							    	_flipview.refreshPage(position);
							    }
							    @Override
							    public void onLoadingCancelled(String imageUri, View view) {
							    	
							    }
							});
					localViewHolder.home_page1_hotnews_title.setText(hotnew.getTitle());
					localViewHolder.loadingPane.setVisibility(View.GONE);
				}
			
			}catch(Exception e){
				
				
			}
			
			
			
			if (this.mListPapers.size() == 0)
			{
								
				localViewHolder.home_shadow_square02.setVisibility(View.GONE);
				localViewHolder.home_img_square02.setImageResource(R.drawable.add_more_220x220);
				//localViewHolder.home_img_square02.setVisibility(View.GONE);
				
				localViewHolder.home_img_square02.setOnClickListener(new View.OnClickListener() {					
					@Override
					public void onClick(View v) {
						Log.v("hom img clicked");
						// TODO Auto-generated method stub
						Intent mIntent = new Intent(context, MainAllPapersActivity.class);			
						mAct.startActivity(mIntent);							
						mAct.overridePendingTransition(R.anim.slide_in_right, R.anim.no_animation);
					}
				});
			}
			else
			{				
				Paper pp =  this.mListPapers.get(0);
				localViewHolder.home_txt_square02.setText(pp.getTitle());
				ImageLoader.getInstance().displayImage("http://news.allmobile.vn/logo/" + pp.getImage(), localViewHolder.home_img_square02,
						ImageLoadOption.options, new ImageLoadingListener() {
						    @Override
						    public void onLoadingStarted(String imageUri, View view) {
						    	Log.v("loading...");				    
						    }
						    @Override
						    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
						    	((ImageView) view).setImageResource(R.drawable.no_image_220x220);
						    	_flipview.refreshPage(position);
						    }
						    @Override
						    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
						    	_flipview.refreshPage(position);
						    }
						    @Override
						    public void onLoadingCancelled(String imageUri, View view) {
						    	
						    }
						});	
				if(NetworkChangeReceiver.isConnected(true))
					localViewHolder.refreshButton.setVisibility(View.VISIBLE);
				
				localViewHolder.home_img_square02.setOnClickListener(new onUserPaperClickListenner(0,context,mAct));
				if(pp.isUpdating()){
					localViewHolder.home_loading_square02.setVisibility(View.VISIBLE);
				}
				else
				{
					localViewHolder.home_loading_square02.setVisibility(View.GONE);
				}
				
			}
		
		}
		else
		{
			setViewVisibility(localViewHolder, localView);
			localViewHolder.square_wrap.setVisibility(View.VISIBLE);
			localViewHolder.home_page1_hotnews_wrap.setVisibility(View.GONE);
			localViewHolder.square_txt_name.setText("BÁO YÊU THÍCH");
			localViewHolder.grid_item_page.setText("Trang " +(position + 1)+ "/"+ getCount());
			if(NetworkChangeReceiver.isConnected(true))
				localViewHolder.refreshButton.setVisibility(View.VISIBLE);
			int i=0;
			try{
				for(i=0;i<6;i++)
				{ 
					Paper pp = null;
					int itemIndex = (position -1)*6 + i +1;
					Log.v("intemIndex:" + itemIndex );
					pp = this.mListPapers.get(itemIndex);
					Log.v("pp title"+pp.getTitle());
					try{
						if(pp !=null)
						{
							localViewHolder.shadow_square[i].setVisibility(1);
							localViewHolder.txt_square[i].setVisibility(1);
							localViewHolder.img_square[i].setVisibility(1);
							localViewHolder.img_square[i].setOnClickListener(new onUserPaperClickListenner(itemIndex,context,mAct));
							localViewHolder.txt_square[i].setText(pp.getTitle());
							if(pp.isUpdating()){
								localViewHolder.loading[i].setVisibility(View.VISIBLE);
							}
							else
							{
								localViewHolder.loading[i].setVisibility(View.GONE);
							}
							ImageLoader.getInstance().displayImage("http://news.allmobile.vn/logo/"+pp.getImage(), localViewHolder.img_square[i],ImageLoadOption.options, new ImageLoadingListener() {
							    @Override
							    public void onLoadingStarted(String imageUri, View view) {
							    	Log.v("loading...");				    
							    }
							    @Override
							    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
							    	((ImageView) view).setImageResource(R.drawable.no_image_220x220);
							    	_flipview.refreshPage(position);
							    }
							    @Override
							    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
							    	_flipview.refreshPage(position);
							    }
							    @Override
							    public void onLoadingCancelled(String imageUri, View view) {
							    	
							    }
							});
						}
					}catch(Exception e){
						
						localViewHolder.txt_square[i].setVisibility(View.GONE);
						localViewHolder.img_square[i].setVisibility(View.GONE);
						localViewHolder.shadow_square[i].setVisibility(View.GONE);
						
					}
			
				}
			
			}catch(Exception e){				
				e.printStackTrace();
				localViewHolder.txt_square[i].setVisibility(View.GONE);
				localViewHolder.img_square[i].setVisibility(View.GONE);
				localViewHolder.shadow_square[i].setVisibility(View.GONE);
			}
			Log.v("last i:"+i);
			
			if(position == getCount()-1)
			{
				localViewHolder.shadow_square[i].setVisibility(View.GONE);
				localViewHolder.img_square[i].setImageResource(R.drawable.add_more_220x220);
				localViewHolder.img_square[i].setVisibility(View.VISIBLE);
				localViewHolder.img_square[i].setOnClickListener(new View.OnClickListener() {					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent mIntent = new Intent(context, MainAllPapersActivity.class);			
						mAct.startActivity(mIntent);
						
						mAct.overridePendingTransition(R.anim.slide_in_right, R.anim.no_animation);
					}
				});
				
			}
					
			
		}
	
		return localView;
		
	}

	
	
	private void setViewVisibility(ViewHolder paramViewHolder, View paramView)
    {
      for(int i=0;i< 6;i++)
      {
    	  paramViewHolder.txt_square[i].setVisibility(View.GONE);
    	  paramViewHolder.img_square[i].setVisibility(View.GONE);
    	  paramViewHolder.shadow_square[i].setVisibility(View.GONE);
      }
    }
	static class ViewHolder{
		View home_page1_hotnews_wrap;
		View square_wrap;
		View loadingPane;
		ImageView home_page1_hotnews_image;
		TextView home_page1_hotnews_title;
		TextView  home_txt_square02 ;
		ImageView home_img_square02;
		ImageView  home_shadow_square02;
		ImageView  home_page1_refreshButton;
		View home_loading_square02;
		TextView square_txt_name;
		
		TextView[]  txt_square = new TextView[6];
		ImageView[] img_square = new ImageView[6];		
		ImageView[] shadow_square = new ImageView[6];
		View[] loading = new View[6];
		
		TextView grid_item_nointernet;		
		TextView grid_item_page;
		ImageView refreshButton;
		ImageView settingButton;
		ImageView bookmarkButton;
		ImageView list_back_button;
	
	}
	@Override
	public void onClick(View v) {
		Log.v("clicked");
		switch(v.getId()){
		case R.id.home_page1_hotnews_image:
			Log.v("clicked");
			break;
		
		}
	}
	
}
